# don't forget to load the packages
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
dat <- read.csv('nba2017-players.csv', stringsAsFactors = FALSE)
# first three rows
three_rows <- slice(dat, 1:3)
three_rows
## # A tibble: 3 x 15
## player team position height weight age experience
## <chr> <chr> <chr> <int> <int> <int> <int>
## 1 Al Horford BOS C 82 245 30 9
## 2 Amir Johnson BOS PF 81 240 29 11
## 3 Avery Bradley BOS SG 74 180 26 6
## # ... with 8 more variables: college <chr>, salary <dbl>, games <int>,
## # minutes <int>, points <int>, points3 <int>, points2 <int>,
## # points1 <int>
# subset rows given a condition
# (height greater than 85 inches)
gt_85 <- filter(dat, height > 85)
gt_85
## player team position height weight age experience
## 1 Edy Tavares CLE C 87 260 24 1
## 2 Boban Marjanovic DET C 87 290 28 1
## 3 Kristaps Porzingis NYK PF 87 240 21 1
## 4 Roy Hibbert DEN C 86 270 30 8
## 5 Alexis Ajinca NOP C 86 248 28 6
## college salary games minutes points points3 points2
## 1 5145 1 24 6 0 3
## 2 7000000 35 293 191 0 72
## 3 4317720 66 2164 1196 112 331
## 4 Georgetown University 5000000 6 11 4 0 2
## 5 4600000 39 584 207 0 89
## points1
## 1 0
## 2 47
## 3 198
## 4 0
## 5 29
# columns by name
player_height <- select(dat, player, height)
player_height
## player height
## 1 Al Horford 82
## 2 Amir Johnson 81
## 3 Avery Bradley 74
## 4 Demetrius Jackson 73
## 5 Gerald Green 79
## 6 Isaiah Thomas 69
## 7 Jae Crowder 78
## 8 James Young 78
## 9 Jaylen Brown 79
## 10 Jonas Jerebko 82
## 11 Jordan Mickey 80
## 12 Kelly Olynyk 84
## 13 Marcus Smart 76
## 14 Terry Rozier 74
## 15 Tyler Zeller 84
## 16 Channing Frye 83
## 17 Dahntay Jones 78
## 18 Deron Williams 75
## 19 Derrick Williams 80
## 20 Edy Tavares 87
## 21 Iman Shumpert 77
## 22 J.R. Smith 78
## 23 James Jones 80
## 24 Kay Felder 69
## 25 Kevin Love 82
## 26 Kyle Korver 79
## 27 Kyrie Irving 75
## 28 LeBron James 80
## 29 Richard Jefferson 79
## 30 Tristan Thompson 81
## 31 Bruno Caboclo 81
## 32 Cory Joseph 75
## 33 Delon Wright 77
## 34 DeMar DeRozan 79
## 35 DeMarre Carroll 80
## 36 Fred VanVleet 72
## 37 Jakob Poeltl 84
## 38 Jonas Valanciunas 84
## 39 Kyle Lowry 72
## 40 Lucas Nogueira 84
## 41 Norman Powell 76
## 42 P.J. Tucker 78
## 43 Pascal Siakam 81
## 44 Patrick Patterson 81
## 45 Serge Ibaka 82
## 46 Bojan Bogdanovic 80
## 47 Bradley Beal 77
## 48 Brandon Jennings 73
## 49 Chris McCullough 83
## 50 Daniel Ochefu 83
## 51 Ian Mahinmi 83
## 52 Jason Smith 84
## 53 John Wall 76
## 54 Marcin Gortat 83
## 55 Markieff Morris 82
## 56 Otto Porter 80
## 57 Sheldon McClellan 77
## 58 Tomas Satoransky 79
## 59 Trey Burke 73
## 60 DeAndre' Bembry 78
## 61 Dennis Schroder 73
## 62 Dwight Howard 83
## 63 Ersan Ilyasova 82
## 64 Jose Calderon 75
## 65 Kent Bazemore 77
## 66 Kris Humphries 81
## 67 Malcolm Delaney 75
## 68 Mike Dunleavy 81
## 69 Mike Muscala 83
## 70 Paul Millsap 80
## 71 Ryan Kelly 83
## 72 Thabo Sefolosha 79
## 73 Tim Hardaway 78
## 74 Giannis Antetokounmpo 83
## 75 Greg Monroe 83
## 76 Jabari Parker 80
## 77 Jason Terry 74
## 78 John Henson 83
## 79 Khris Middleton 80
## 80 Malcolm Brogdon 77
## 81 Matthew Dellavedova 76
## 82 Michael Beasley 81
## 83 Mirza Teletovic 81
## 84 Rashad Vaughn 78
## 85 Spencer Hawes 85
## 86 Thon Maker 85
## 87 Tony Snell 79
## 88 Aaron Brooks 72
## 89 Al Jefferson 82
## 90 C.J. Miles 78
## 91 Georges Niang 80
## 92 Jeff Teague 74
## 93 Joe Young 74
## 94 Kevin Seraphin 81
## 95 Lance Stephenson 77
## 96 Lavoy Allen 81
## 97 Monta Ellis 75
## 98 Myles Turner 83
## 99 Paul George 81
## 100 Rakeem Christmas 81
## 101 Thaddeus Young 80
## 102 Anthony Morrow 77
## 103 Bobby Portis 83
## 104 Cameron Payne 75
## 105 Cristiano Felicio 82
## 106 Denzel Valentine 78
## 107 Dwyane Wade 76
## 108 Isaiah Canaan 72
## 109 Jerian Grant 76
## 110 Jimmy Butler 79
## 111 Joffrey Lauvergne 83
## 112 Michael Carter-Williams 78
## 113 Nikola Mirotic 82
## 114 Paul Zipser 80
## 115 Rajon Rondo 73
## 116 Robin Lopez 84
## 117 Dion Waiters 76
## 118 Goran Dragic 75
## 119 Hassan Whiteside 84
## 120 James Johnson 81
## 121 Josh McRoberts 82
## 122 Josh Richardson 78
## 123 Justise Winslow 79
## 124 Luke Babbitt 81
## 125 Okaro White 80
## 126 Rodney McGruder 76
## 127 Tyler Johnson 76
## 128 Udonis Haslem 80
## 129 Wayne Ellington 76
## 130 Willie Reed 82
## 131 Andre Drummond 83
## 132 Aron Baynes 82
## 133 Beno Udrih 75
## 134 Boban Marjanovic 87
## 135 Darrun Hilliard 78
## 136 Henry Ellenson 83
## 137 Ish Smith 72
## 138 Jon Leuer 82
## 139 Kentavious Caldwell-Pope 77
## 140 Marcus Morris 81
## 141 Michael Gbinije 79
## 142 Reggie Bullock 79
## 143 Reggie Jackson 75
## 144 Stanley Johnson 79
## 145 Tobias Harris 81
## 146 Brian Roberts 73
## 147 Briante Weber 74
## 148 Christian Wood 83
## 149 Cody Zeller 84
## 150 Frank Kaminsky 84
## 151 Jeremy Lamb 77
## 152 Johnny O'Bryant 81
## 153 Kemba Walker 73
## 154 Marco Belinelli 77
## 155 Marvin Williams 81
## 156 Michael Kidd-Gilchrist 79
## 157 Miles Plumlee 83
## 158 Nicolas Batum 80
## 159 Ramon Sessions 75
## 160 Treveon Graham 78
## 161 Carmelo Anthony 80
## 162 Chasson Randle 74
## 163 Courtney Lee 77
## 164 Derrick Rose 75
## 165 Joakim Noah 83
## 166 Justin Holiday 78
## 167 Kristaps Porzingis 87
## 168 Kyle O'Quinn 82
## 169 Lance Thomas 80
## 170 Marshall Plumlee 84
## 171 Maurice Ndour 81
## 172 Mindaugas Kuzminskas 81
## 173 Ron Baker 76
## 174 Sasha Vujacic 79
## 175 Willy Hernangomez 83
## 176 Aaron Gordon 81
## 177 Bismack Biyombo 81
## 178 C.J. Watson 74
## 179 D.J. Augustin 72
## 180 Damjan Rudez 82
## 181 Elfrid Payton 76
## 182 Evan Fournier 79
## 183 Jeff Green 81
## 184 Jodie Meeks 76
## 185 Marcus Georges-Hunt 77
## 186 Mario Hezonja 80
## 187 Nikola Vucevic 84
## 188 Patricio Garino 78
## 189 Stephen Zimmerman 84
## 190 Terrence Ross 79
## 191 Alex Poythress 79
## 192 Dario Saric 82
## 193 Gerald Henderson 77
## 194 Jahlil Okafor 83
## 195 Jerryd Bayless 75
## 196 Joel Embiid 84
## 197 Justin Anderson 78
## 198 Nik Stauskas 78
## 199 Richaun Holmes 82
## 200 Robert Covington 81
## 201 Sergio Rodriguez 75
## 202 Shawn Long 81
## 203 T.J. McConnell 74
## 204 Tiago Splitter 83
## 205 Timothe Luwawu-Cabarrot 78
## 206 Andrew Nicholson 81
## 207 Archie Goodwin 77
## 208 Brook Lopez 84
## 209 Caris LeVert 79
## 210 Isaiah Whitehead 76
## 211 Jeremy Lin 75
## 212 Joe Harris 78
## 213 Justin Hamilton 84
## 214 K.J. McDaniels 78
## 215 Quincy Acy 79
## 216 Randy Foye 76
## 217 Rondae Hollis-Jefferson 79
## 218 Sean Kilpatrick 76
## 219 Spencer Dinwiddie 78
## 220 Trevor Booker 80
## 221 Andre Iguodala 78
## 222 Damian Jones 84
## 223 David West 81
## 224 Draymond Green 79
## 225 Ian Clark 75
## 226 James Michael McAdoo 81
## 227 JaVale McGee 84
## 228 Kevin Durant 81
## 229 Kevon Looney 81
## 230 Klay Thompson 79
## 231 Matt Barnes 79
## 232 Patrick McCaw 79
## 233 Shaun Livingston 79
## 234 Stephen Curry 75
## 235 Zaza Pachulia 83
## 236 Bryn Forbes 75
## 237 Danny Green 78
## 238 David Lee 81
## 239 Davis Bertans 82
## 240 Dejounte Murray 77
## 241 Dewayne Dedmon 84
## 242 Joel Anthony 81
## 243 Jonathon Simmons 78
## 244 Kawhi Leonard 79
## 245 Kyle Anderson 81
## 246 LaMarcus Aldridge 83
## 247 Manu Ginobili 78
## 248 Patty Mills 72
## 249 Pau Gasol 84
## 250 Tony Parker 74
## 251 Bobby Brown 74
## 252 Chinanu Onuaku 82
## 253 Clint Capela 82
## 254 Eric Gordon 76
## 255 Isaiah Taylor 75
## 256 James Harden 77
## 257 Kyle Wiltjer 82
## 258 Lou Williams 73
## 259 Montrezl Harrell 80
## 260 Patrick Beverley 73
## 261 Ryan Anderson 82
## 262 Sam Dekker 81
## 263 Trevor Ariza 80
## 264 Troy Williams 79
## 265 Alan Anderson 78
## 266 Austin Rivers 76
## 267 Blake Griffin 82
## 268 Brandon Bass 80
## 269 Brice Johnson 82
## 270 Chris Paul 72
## 271 DeAndre Jordan 83
## 272 Diamond Stone 83
## 273 J.J. Redick 76
## 274 Jamal Crawford 77
## 275 Luc Mbah a Moute 80
## 276 Marreese Speights 82
## 277 Paul Pierce 79
## 278 Raymond Felton 73
## 279 Wesley Johnson 79
## 280 Alec Burks 78
## 281 Boris Diaw 80
## 282 Dante Exum 78
## 283 Derrick Favors 82
## 284 George Hill 75
## 285 Gordon Hayward 80
## 286 Jeff Withey 84
## 287 Joe Ingles 80
## 288 Joe Johnson 79
## 289 Joel Bolomboy 81
## 290 Raul Neto 73
## 291 Rodney Hood 80
## 292 Rudy Gobert 85
## 293 Shelvin Mack 75
## 294 Trey Lyles 82
## 295 Alex Abrines 78
## 296 Andre Roberson 79
## 297 Domantas Sabonis 83
## 298 Doug McDermott 80
## 299 Enes Kanter 83
## 300 Jerami Grant 80
## 301 Josh Huestis 79
## 302 Kyle Singler 80
## 303 Nick Collison 82
## 304 Norris Cole 74
## 305 Russell Westbrook 75
## 306 Semaj Christon 75
## 307 Steven Adams 84
## 308 Taj Gibson 81
## 309 Victor Oladipo 76
## 310 Andrew Harrison 78
## 311 Brandan Wright 82
## 312 Chandler Parsons 82
## 313 Deyonta Davis 83
## 314 James Ennis 79
## 315 JaMychal Green 81
## 316 Jarell Martin 82
## 317 Marc Gasol 85
## 318 Mike Conley 73
## 319 Tony Allen 76
## 320 Troy Daniels 76
## 321 Vince Carter 78
## 322 Wade Baldwin 76
## 323 Wayne Selden 77
## 324 Zach Randolph 81
## 325 Al-Farouq Aminu 81
## 326 Allen Crabbe 78
## 327 C.J. McCollum 76
## 328 Damian Lillard 75
## 329 Ed Davis 82
## 330 Evan Turner 79
## 331 Jake Layman 81
## 332 Jusuf Nurkic 84
## 333 Maurice Harkless 81
## 334 Meyers Leonard 85
## 335 Noah Vonleh 82
## 336 Pat Connaughton 77
## 337 Shabazz Napier 73
## 338 Tim Quarterman 78
## 339 Danilo Gallinari 82
## 340 Darrell Arthur 81
## 341 Emmanuel Mudiay 77
## 342 Gary Harris 76
## 343 Jamal Murray 76
## 344 Jameer Nelson 72
## 345 Juan Hernangomez 81
## 346 Kenneth Faried 80
## 347 Malik Beasley 77
## 348 Mason Plumlee 83
## 349 Mike Miller 80
## 350 Nikola Jokic 82
## 351 Roy Hibbert 86
## 352 Will Barton 78
## 353 Wilson Chandler 80
## 354 Alexis Ajinca 86
## 355 Anthony Davis 82
## 356 Axel Toupane 79
## 357 Cheick Diallo 81
## 358 Dante Cunningham 80
## 359 DeMarcus Cousins 83
## 360 Donatas Motiejunas 84
## 361 E'Twaun Moore 76
## 362 Jordan Crawford 76
## 363 Jrue Holiday 76
## 364 Omer Asik 84
## 365 Quinn Cook 74
## 366 Solomon Hill 79
## 367 Tim Frazier 73
## 368 A.J. Hammons 84
## 369 DeAndre Liggins 78
## 370 Devin Harris 75
## 371 Dirk Nowitzki 84
## 372 Dorian Finney-Smith 80
## 373 Dwight Powell 83
## 374 Harrison Barnes 80
## 375 J.J. Barea 72
## 376 Jarrod Uthoff 81
## 377 Nerlens Noel 83
## 378 Nicolas Brussino 79
## 379 Salah Mejri 85
## 380 Seth Curry 74
## 381 Wesley Matthews 77
## 382 Yogi Ferrell 72
## 383 Anthony Tolliver 80
## 384 Arron Afflalo 77
## 385 Ben McLemore 77
## 386 Buddy Hield 76
## 387 Darren Collison 72
## 388 Garrett Temple 78
## 389 Georgios Papagiannis 85
## 390 Kosta Koufos 84
## 391 Langston Galloway 74
## 392 Malachi Richardson 78
## 393 Rudy Gay 80
## 394 Skal Labissiere 83
## 395 Ty Lawson 71
## 396 Tyreke Evans 78
## 397 Willie Cauley-Stein 84
## 398 Adreian Payne 82
## 399 Andrew Wiggins 80
## 400 Brandon Rush 78
## 401 Cole Aldrich 83
## 402 Gorgui Dieng 83
## 403 Jordan Hill 82
## 404 Karl-Anthony Towns 84
## 405 Kris Dunn 76
## 406 Nemanja Bjelica 82
## 407 Omri Casspi 81
## 408 Ricky Rubio 76
## 409 Shabazz Muhammad 78
## 410 Tyus Jones 74
## 411 Zach LaVine 77
## 412 Brandon Ingram 81
## 413 Corey Brewer 81
## 414 D'Angelo Russell 77
## 415 David Nwaba 76
## 416 Ivica Zubac 85
## 417 Jordan Clarkson 77
## 418 Julius Randle 81
## 419 Larry Nance Jr. 81
## 420 Luol Deng 81
## 421 Metta World Peace 78
## 422 Nick Young 79
## 423 Tarik Black 81
## 424 Thomas Robinson 82
## 425 Timofey Mozgov 85
## 426 Tyler Ennis 75
## 427 Alan Williams 80
## 428 Alex Len 85
## 429 Brandon Knight 75
## 430 Derrick Jones 79
## 431 Devin Booker 78
## 432 Dragan Bender 85
## 433 Elijah Millsap 78
## 434 Eric Bledsoe 73
## 435 Jared Dudley 79
## 436 Leandro Barbosa 75
## 437 Marquese Chriss 82
## 438 Ronnie Price 74
## 439 T.J. Warren 80
## 440 Tyler Ulis 70
## 441 Tyson Chandler 85
five_rows <- slice(dat, 1:5)
five_rows
## # A tibble: 5 x 15
## player team position height weight age experience
## <chr> <chr> <chr> <int> <int> <int> <int>
## 1 Al Horford BOS C 82 245 30 9
## 2 Amir Johnson BOS PF 81 240 29 11
## 3 Avery Bradley BOS SG 74 180 26 6
## 4 Demetrius Jackson BOS PG 73 201 22 0
## 5 Gerald Green BOS SF 79 205 31 9
## # ... with 8 more variables: college <chr>, salary <dbl>, games <int>,
## # minutes <int>, points <int>, points3 <int>, points2 <int>,
## # points1 <int>
slice(dat, seq(10, 50, 5))
## # A tibble: 9 x 15
## player team position height weight age experience
## <chr> <chr> <chr> <int> <int> <int> <int>
## 1 Jonas Jerebko BOS PF 82 231 29 6
## 2 Tyler Zeller BOS C 84 253 27 4
## 3 Edy Tavares CLE C 87 260 24 1
## 4 Kevin Love CLE PF 82 251 28 8
## 5 Tristan Thompson CLE C 81 238 25 5
## 6 DeMarre Carroll TOR SF 80 215 30 7
## 7 Lucas Nogueira TOR C 84 241 24 2
## 8 Serge Ibaka TOR PF 82 235 27 7
## 9 Daniel Ochefu WAS C 83 245 23 0
## # ... with 8 more variables: college <chr>, salary <dbl>, games <int>,
## # minutes <int>, points <int>, points3 <int>, points2 <int>,
## # points1 <int>
slice(dat, 437:441)
## # A tibble: 5 x 15
## player team position height weight age experience
## <chr> <chr> <chr> <int> <int> <int> <int>
## 1 Marquese Chriss PHO PF 82 233 19 0
## 2 Ronnie Price PHO PG 74 190 33 11
## 3 T.J. Warren PHO SF 80 230 23 2
## 4 Tyler Ulis PHO PG 70 150 21 0
## 5 Tyson Chandler PHO C 85 240 34 15
## # ... with 8 more variables: college <chr>, salary <dbl>, games <int>,
## # minutes <int>, points <int>, points3 <int>, points2 <int>,
## # points1 <int>
filter(dat, height < 70)
## player team position height weight age experience
## 1 Isaiah Thomas BOS PG 69 185 27 5
## 2 Kay Felder CLE PG 69 176 21 0
## college salary games minutes points points3 points2
## 1 University of Washington 6587132 76 2569 2199 245 437
## 2 Oakland University 543471 42 386 166 7 55
## points1
## 1 590
## 2 35
filter(dat, team == 'GSW')
## player team position height weight age experience
## 1 Andre Iguodala GSW SF 78 215 33 12
## 2 Damian Jones GSW C 84 245 21 0
## 3 David West GSW C 81 250 36 13
## 4 Draymond Green GSW PF 79 230 26 4
## 5 Ian Clark GSW SG 75 175 25 3
## 6 James Michael McAdoo GSW PF 81 230 24 2
## 7 JaVale McGee GSW C 84 270 29 8
## 8 Kevin Durant GSW SF 81 240 28 9
## 9 Kevon Looney GSW C 81 220 20 1
## 10 Klay Thompson GSW SG 79 215 26 5
## 11 Matt Barnes GSW SF 79 226 36 13
## 12 Patrick McCaw GSW SG 79 185 21 0
## 13 Shaun Livingston GSW PG 79 192 31 11
## 14 Stephen Curry GSW PG 75 190 28 7
## 15 Zaza Pachulia GSW C 83 270 32 13
## college salary games minutes points
## 1 University of Arizona 11131368 76 1998 574
## 2 Vanderbilt University 1171560 10 85 19
## 3 Xavier University 1551659 68 854 316
## 4 Michigan State University 15330435 76 2471 776
## 5 Belmont University 1015696 77 1137 527
## 6 University of North Carolina 980431 52 457 147
## 7 University of Nevada, Reno 1403611 77 739 472
## 8 University of Texas at Austin 26540100 62 2070 1555
## 9 University of California, Los Angeles 1182840 53 447 135
## 10 Washington State University 16663575 78 2649 1742
## 11 University of California, Los Angeles 383351 20 410 114
## 12 University of Nevada, Las Vegas 543471 71 1074 282
## 13 5782450 76 1345 389
## 14 Davidson College 12112359 79 2638 1999
## 15 2898000 70 1268 426
## points3 points2 points1
## 1 64 155 72
## 2 0 8 3
## 3 3 132 43
## 4 81 191 151
## 5 61 150 44
## 6 2 60 21
## 7 0 208 56
## 8 117 434 336
## 9 2 54 21
## 10 268 376 186
## 11 18 20 20
## 12 41 65 29
## 13 1 172 42
## 14 324 351 325
## 15 0 164 98
filter(dat, team == 'GSW' & position == 'C')
## player team position height weight age experience
## 1 Damian Jones GSW C 84 245 21 0
## 2 David West GSW C 81 250 36 13
## 3 JaVale McGee GSW C 84 270 29 8
## 4 Kevon Looney GSW C 81 220 20 1
## 5 Zaza Pachulia GSW C 83 270 32 13
## college salary games minutes points
## 1 Vanderbilt University 1171560 10 85 19
## 2 Xavier University 1551659 68 854 316
## 3 University of Nevada, Reno 1403611 77 739 472
## 4 University of California, Los Angeles 1182840 53 447 135
## 5 2898000 70 1268 426
## points3 points2 points1
## 1 0 8 3
## 2 3 132 43
## 3 0 208 56
## 4 2 54 21
## 5 0 164 98
select(filter(dat, team == 'LAL'), player)
## player
## 1 Brandon Ingram
## 2 Corey Brewer
## 3 D'Angelo Russell
## 4 David Nwaba
## 5 Ivica Zubac
## 6 Jordan Clarkson
## 7 Julius Randle
## 8 Larry Nance Jr.
## 9 Luol Deng
## 10 Metta World Peace
## 11 Nick Young
## 12 Tarik Black
## 13 Thomas Robinson
## 14 Timofey Mozgov
## 15 Tyler Ennis
select(filter(dat, team == 'GSW' & position == 'PG'), player, salary)
## player salary
## 1 Shaun Livingston 5782450
## 2 Stephen Curry 12112359
select(filter(dat, experience > 10 & salary < 10000000), player, age, team)
## player age team
## 1 Dahntay Jones 36 CLE
## 2 Deron Williams 32 CLE
## 3 James Jones 36 CLE
## 4 Kyle Korver 35 CLE
## 5 Richard Jefferson 36 CLE
## 6 Jose Calderon 35 ATL
## 7 Kris Humphries 31 ATL
## 8 Mike Dunleavy 36 ATL
## 9 Jason Terry 39 MIL
## 10 C.J. Miles 29 IND
## 11 Udonis Haslem 36 MIA
## 12 Beno Udrih 34 DET
## 13 David West 36 GSW
## 14 Matt Barnes 36 GSW
## 15 Shaun Livingston 31 GSW
## 16 Zaza Pachulia 32 GSW
## 17 David Lee 33 SAS
## 18 Lou Williams 30 HOU
## 19 Trevor Ariza 31 HOU
## 20 Brandon Bass 31 LAC
## 21 Paul Pierce 39 LAC
## 22 Raymond Felton 32 LAC
## 23 Boris Diaw 34 UTA
## 24 Nick Collison 36 OKC
## 25 Tony Allen 35 MEM
## 26 Vince Carter 40 MEM
## 27 Jameer Nelson 34 DEN
## 28 Mike Miller 36 DEN
## 29 Devin Harris 33 DAL
## 30 Metta World Peace 37 LAL
## 31 Leandro Barbosa 34 PHO
## 32 Ronnie Price 33 PHO
slice(select(filter(dat, age == 20 & experience == 0), player, team ,height, weight), 0:5)
## # A tibble: 5 x 4
## player team height weight
## <chr> <chr> <int> <int>
## 1 <NA> <NA> NA NA
## 2 Jaylen Brown BOS 79 225
## 3 Henry Ellenson DET 83 245
## 4 Stephen Zimmerman ORL 84 240
## 5 Dejounte Murray SAS 77 170
# creating a small data frame step by step
gsw <- filter(dat, team == 'GSW')
gsw <- select(gsw, player, height, weight)
gsw <- slice(gsw, c(4, 8, 10, 14, 15))
gsw
## # A tibble: 5 x 3
## player height weight
## <chr> <int> <int>
## 1 Draymond Green 79 230
## 2 Kevin Durant 81 240
## 3 Klay Thompson 79 215
## 4 Stephen Curry 75 190
## 5 Zaza Pachulia 83 270
mutate(gsw, height / weight)
## # A tibble: 5 x 4
## player height weight `height/weight`
## <chr> <int> <int> <dbl>
## 1 Draymond Green 79 230 0.3434783
## 2 Kevin Durant 81 240 0.3375000
## 3 Klay Thompson 79 215 0.3674419
## 4 Stephen Curry 75 190 0.3947368
## 5 Zaza Pachulia 83 270 0.3074074
mutate(gsw, ht_wt = height / weight)
## # A tibble: 5 x 4
## player height weight ht_wt
## <chr> <int> <int> <dbl>
## 1 Draymond Green 79 230 0.3434783
## 2 Kevin Durant 81 240 0.3375000
## 3 Klay Thompson 79 215 0.3674419
## 4 Stephen Curry 75 190 0.3947368
## 5 Zaza Pachulia 83 270 0.3074074
gsw2 <- mutate(gsw, ht_m = height * 0.0254, wt_kg = weight * 0.4536)
gsw2
## # A tibble: 5 x 5
## player height weight ht_m wt_kg
## <chr> <int> <int> <dbl> <dbl>
## 1 Draymond Green 79 230 2.0066 104.328
## 2 Kevin Durant 81 240 2.0574 108.864
## 3 Klay Thompson 79 215 2.0066 97.524
## 4 Stephen Curry 75 190 1.9050 86.184
## 5 Zaza Pachulia 83 270 2.1082 122.472
# order rows by height (increasingly)
arrange(gsw, height)
## # A tibble: 5 x 3
## player height weight
## <chr> <int> <int>
## 1 Stephen Curry 75 190
## 2 Draymond Green 79 230
## 3 Klay Thompson 79 215
## 4 Kevin Durant 81 240
## 5 Zaza Pachulia 83 270
# order rows by height (decreasingly)
arrange(gsw, desc(height))
## # A tibble: 5 x 3
## player height weight
## <chr> <int> <int>
## 1 Zaza Pachulia 83 270
## 2 Kevin Durant 81 240
## 3 Draymond Green 79 230
## 4 Klay Thompson 79 215
## 5 Stephen Curry 75 190
# order rows by height, and then weight
arrange(gsw, height, weight)
## # A tibble: 5 x 3
## player height weight
## <chr> <int> <int>
## 1 Stephen Curry 75 190
## 2 Klay Thompson 79 215
## 3 Draymond Green 79 230
## 4 Kevin Durant 81 240
## 5 Zaza Pachulia 83 270
mutate(gsw, product=height*weight)
## # A tibble: 5 x 4
## player height weight product
## <chr> <int> <int> <int>
## 1 Draymond Green 79 230 18170
## 2 Kevin Durant 81 240 19440
## 3 Klay Thompson 79 215 16985
## 4 Stephen Curry 75 190 14250
## 5 Zaza Pachulia 83 270 22410
gsw3 <- mutate(gsw, log_height=log(height), log_weight=log(weight))
gsw3
## # A tibble: 5 x 5
## player height weight log_height log_weight
## <chr> <int> <int> <dbl> <dbl>
## 1 Draymond Green 79 230 4.369448 5.438079
## 2 Kevin Durant 81 240 4.394449 5.480639
## 3 Klay Thompson 79 215 4.369448 5.370638
## 4 Stephen Curry 75 190 4.317488 5.247024
## 5 Zaza Pachulia 83 270 4.418841 5.598422
arrange(filter(dat, height < 71),height)
## player team position height weight age experience
## 1 Isaiah Thomas BOS PG 69 185 27 5
## 2 Kay Felder CLE PG 69 176 21 0
## 3 Tyler Ulis PHO PG 70 150 21 0
## college salary games minutes points points3 points2
## 1 University of Washington 6587132 76 2569 2199 245 437
## 2 Oakland University 543471 42 386 166 7 55
## 3 University of Kentucky 918369 61 1123 444 21 163
## points1
## 1 590
## 2 35
## 3 55
slice(arrange(select(dat, player, team, salary), desc(salary)), 0:5)
## # A tibble: 5 x 3
## player team salary
## <chr> <chr> <dbl>
## 1 <NA> <NA> NA
## 2 LeBron James CLE 30963450
## 3 Al Horford BOS 26540100
## 4 DeMar DeRozan TOR 26540100
## 5 Kevin Durant GSW 26540100
slice(arrange(select(dat, player, team, points3), desc(points3)), 0:10)
## # A tibble: 10 x 3
## player team points3
## <chr> <chr> <int>
## 1 <NA> <NA> NA
## 2 Stephen Curry GSW 324
## 3 Klay Thompson GSW 268
## 4 James Harden HOU 262
## 5 Eric Gordon HOU 246
## 6 Isaiah Thomas BOS 245
## 7 Kemba Walker CHO 240
## 8 Bradley Beal WAS 223
## 9 Damian Lillard POR 214
## 10 Ryan Anderson HOU 204
gsw_mpg <- arrange(select(mutate(filter(dat, team == 'GSW'), min_per_game = minutes/games), player, experience, min_per_game), desc(min_per_game))
gsw_mpg
## player experience min_per_game
## 1 Klay Thompson 5 33.961538
## 2 Stephen Curry 7 33.392405
## 3 Kevin Durant 9 33.387097
## 4 Draymond Green 4 32.513158
## 5 Andre Iguodala 12 26.289474
## 6 Matt Barnes 13 20.500000
## 7 Zaza Pachulia 13 18.114286
## 8 Shaun Livingston 11 17.697368
## 9 Patrick McCaw 0 15.126761
## 10 Ian Clark 3 14.766234
## 11 David West 13 12.558824
## 12 JaVale McGee 8 9.597403
## 13 James Michael McAdoo 2 8.788462
## 14 Damian Jones 0 8.500000
## 15 Kevon Looney 1 8.433962
# average salary of NBA players
summarise(dat, avg_salary = mean(salary))
## avg_salary
## 1 6187014
summarise(
dat,
min = min(salary),
median = median(salary),
avg = mean(salary),
max = max(salary)
)
## min median avg max
## 1 5145 3500000 6187014 30963450
summarise(
group_by(dat, team),
avg_salary = mean(salary)
)
## # A tibble: 30 x 2
## team avg_salary
## <chr> <dbl>
## 1 ATL 6491892
## 2 BOS 6127673
## 3 BRK 4363414
## 4 CHI 6138459
## 5 CHO 6683086
## 6 CLE 8386014
## 7 DAL 6139880
## 8 DEN 5225533
## 9 DET 6871594
## 10 GSW 6579394
## # ... with 20 more rows
summarise(
group_by(dat, position),
avg_salary = mean(salary)
)
## # A tibble: 5 x 2
## position avg_salary
## <chr> <dbl>
## 1 C 6987682
## 2 PF 5890363
## 3 PG 6069029
## 4 SF 6513374
## 5 SG 5535260
summarise(dat, max_salary = max(salary))
## max_salary
## 1 30963450
summarise(dat, sd(points3))
## sd(points3)
## 1 55.9721
summarise(
group_by(dat, team),
median(points3)
)
## # A tibble: 30 x 2
## team `median(points3)`
## <chr> <dbl>
## 1 ATL 32.5
## 2 BOS 46.0
## 3 BRK 44.0
## 4 CHI 32.0
## 5 CHO 17.0
## 6 CLE 62.0
## 7 DAL 53.0
## 8 DEN 53.0
## 9 DET 28.0
## 10 GSW 18.0
## # ... with 20 more rows
summarise(
group_by(dat, team),
mean(points3)
)
## # A tibble: 30 x 2
## team `mean(points3)`
## <chr> <dbl>
## 1 ATL 44.71429
## 2 BOS 65.66667
## 3 BRK 49.20000
## 4 CHI 37.66667
## 5 CHO 53.86667
## 6 CLE 67.46667
## 7 DAL 50.26667
## 8 DEN 57.86667
## 9 DET 42.06667
## 10 GSW 65.46667
## # ... with 20 more rows
summarise(filter(dat, position == 'PF' & experience == 5 | experience == 10), mean(age), sd(age))
## mean(age) sd(age)
## 1 29.35294 2.804933
# scatterplot (option 1)
ggplot(data = dat) +
geom_point(aes(x = points, y = salary))

# scatterplot (option 2)
ggplot(data = dat, aes(x = points, y = salary)) +
geom_point()

# colored scatterplot
ggplot(data = dat, aes(x = points, y = salary)) +
geom_point(aes(color = position))

# sized and colored scatterplot
ggplot(data = dat, aes(x = points, y = salary)) +
geom_point(aes(color = position, size = points3))

# sized and colored scatterplot
ggplot(data = dat, aes(x = points, y = salary)) +
geom_point(aes(color = position, size = points3), alpha = 0.7)

ggplot(data = gsw, aes(x = height, y = weight)) +
geom_point()

ggplot(data = gsw, aes(x = height, y = weight)) +
geom_point()+geom_text(aes(label=player))

ggplot(data = dat, aes(x=salary)) +
geom_density()

ggplot(data=dat, aes(x=points2)) +
geom_histogram(bins=50)

ggplot(data=dat, aes(x=position)) +
geom_bar()

ggplot(data=dat, aes(x=experience, y =salary)) +
geom_point() + geom_smooth()
## `geom_smooth()` using method = 'loess'

ggplot(data=dat, aes(x=experience, y =salary)) +
geom_point() + geom_smooth(method=loess)

# scatterplot by position
ggplot(data = dat, aes(x = points, y = salary)) +
geom_point() +
facet_wrap(~ position)

# scatterplot by position
ggplot(data = dat, aes(x = points, y = salary)) +
geom_point(aes(color = position), alpha = 0.7) +
facet_grid(~ position) +
geom_smooth(method = loess)

# scatterplot by position
ggplot(data = dat, aes(x = points, y = salary)) +
geom_point(aes(color = position), alpha = 0.7) +
facet_grid(position ~ .) +
geom_smooth(method = loess)

ggplot(data = dat, aes(x=experience, y=salary)) +
geom_point() +
facet_wrap(~ position)

ggplot(data = dat, aes(x=experience, y=salary)) +
geom_point() +
facet_wrap(~team)

ggplot(data=dat, aes(x=age)) +
geom_density() +
facet_wrap(~team)

ggplot(data = dat, aes(x=height, y=weight)) +
geom_point() +
facet_wrap(~position)

ggplot(data = dat, aes(x=height, y=weight)) +
geom_density2d() +
facet_wrap(~position)
